<template>
  <BasicModal
    v-bind="$attrs"
    title="比较图档版本"
    @register="register"
    @ok="handleSubmit"
    @cancel="handleCancel"
    :destroyOnClose="true"
    :width="1000"
  >
    <BasicTable
      @register="registerTable"
      :rowSelection="{
        type: 'radio',
        onChange: handleVersionChange,
      }"
    >
      <!-- 创建时间 -->
      <template #createtime="{ text: createtime }">
        <span>
          {{ proxy.$filters.filterTime(createtime) }}
        </span>
      </template>
    </BasicTable>
  </BasicModal>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { Base64 } from 'js-base64';
  import { getCurrentInstance } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicTable, useTable } from '/@/components/Table';
  import { getDocVersionColumns } from './tableData';
  import { GetReleaseFileVersion, ExtractManyFileByAutoView } from '/@/api/demo/document';

  const { proxy }: any = getCurrentInstance();
  const compareDoc = ref<any>({}); // 当前图档
  const comparedDoc = ref<any>({}); // 比较图档

  // 取消
  const handleCancel = () => {
    comparedDoc.value = {};
    comparedDoc.value = {};
    closeModal();
  };

  // 确定
  const handleSubmit = () => {
    let arr: any = [];
    let paramsA = {
      oid: compareDoc.value.oId,
      ver: compareDoc.value.version,
      type: 0,
    };
    let paramsB = {
      oid: comparedDoc.value.oid,
      ver: comparedDoc.value.version,
      type: 0,
    };
    arr.push(paramsA);
    arr.push(paramsB);
    ExtractManyFileByAutoView(arr).then((res) => {
      // doctype为3D图纸，则打开3D预览，否则打开2D预览
      if (res[0].doctype != '3D图纸') {
        let file1 = res[0].viewUrl; //文件1
        let file2 = res[1].viewUrl; //文件2
        console.log(res);
        window.open(
          res[0].serverUrl +
            ':5098/AutoVue/OpenApplet.html?compare2D=true&file1=' +
            encodeURIComponent(Base64.encode(file1)) +
            '&file2=' +
            encodeURIComponent(Base64.encode(file2)),
        );
      } else {
        let file1 = res[0].viewUrl; //文件1
        let file2 = res[1].viewUrl; //文件2
        window.open(
          res[0].serverUrl +
            ':5098/AutoVue/OpenApplet.html?compare3D=true&file1=' +
            encodeURIComponent(Base64.encode(file1)) +
            '&file2=' +
            encodeURIComponent(Base64.encode(file2)),
        );
      }
    });
    handleCancel();
  };

  const handleVersionChange = (selectedRowKeys, selectedRows) => {
    comparedDoc.value = selectedRows[0];
  };

  // 初始化弹窗
  const [register, { setModalProps, closeModal }] = useModalInner((data) => {
    setModalProps({ confirmLoading: false });
    compareDoc.value = data.record;
    GetReleaseFileVersion(data.record.id).then((res) => {
      setTableData(res);
    });
  });

  // 表格初始化
  const [registerTable, { setTableData }] = useTable({
    columns: getDocVersionColumns(),
    bordered: true,
    useSearchForm: false,
    showIndexColumn: false,
    showTableSetting: false, // 显示表格设置工具
    tableSetting: { fullScreen: false }, // 表格设置工具配置
    rowKey: 'version',
    pagination: false,
    maxHeight: 300,
    striped: false,
  });
</script>
